// Manager for logging operations.

#include "LogManager.h"
#include <cstdarg>
#include <string>

// Clear the screen.
void LogManager::ClearScreen()
{
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	DWORD count;
	DWORD cellCount;
	COORD homeCoords = { 0, 0 };

	HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStdOut == INVALID_HANDLE_VALUE) return;

	// get the number of cells in the current buffer
	if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;
	cellCount = csbi.dwSize.X *csbi.dwSize.Y;

	// fill the entire buffer with spaces
	if (!FillConsoleOutputCharacterW(hStdOut, (wchar_t)' ', cellCount, homeCoords, &count)) return;

	// fill the entire buffer with the current colors and attributes
	if (!FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, cellCount, homeCoords, &count)) return;

	// move the cursor home
	SetConsoleCursorPosition(hStdOut, homeCoords);
}

// Display a message and pause.
bool LogManager::DisplayMessageAndPause(_In_z_ _Printf_format_string_ const TCHAR* format, ...)
{
	va_list argList;
	va_start(argList, format);
	vwprintf(format, argList);
	va_end(argList);
	string dummy;
	getline(cin, dummy);
	return true;
}

// Display an error and pause.
bool LogManager::DisplayErrorAndPause(_In_z_ _Printf_format_string_ const TCHAR* format, ...)
{
	va_list argList;
	va_start(argList, format);
	_vtprintf(format, argList);
	va_end(argList);
	string dummy;
	getline(cin, dummy);
	return false;
}

// Display a message.
bool LogManager::DisplayMessage(_In_z_ _Printf_format_string_ const TCHAR* format, ...)
{
	va_list argList;
	va_start(argList, format);
	_vtprintf(format, argList);
	va_end(argList);
	return true;
}

// Display an error.
bool LogManager::DisplayError(_In_z_ _Printf_format_string_ const TCHAR* format, ...)
{
	va_list argList;
	va_start(argList, format);
	_vtprintf(format, argList);
	va_end(argList);
	return false;
}

// Display a warning.
bool LogManager::DisplayWarning(_In_z_ _Printf_format_string_ const TCHAR* format, ...)
{
	format;
#if __SHOW_WARNINGS__
	va_list argList;
	va_start(argList, format);
	_vtprintf(format, argList);
	va_end(argList);
#endif
	return false;
}

// Display an information.
bool LogManager::DisplayInformation(_In_z_ _Printf_format_string_ const TCHAR* format, ...)
{
	format;
#if __SHOW_INFORMATIONS__
	va_list argList;
	va_start(argList, format);
	_vtprintf(format, argList);
	va_end(argList);
#endif
	return true;
}

// Display a message with suspense.
bool LogManager::DisplayWithSuspense(_In_z_ _Printf_format_string_ const TCHAR* format, ...)
{
	va_list argList;
	va_start(argList, format);
	_vtprintf(format, argList);
	va_end(argList);
	Sleep(__SUSPENSE_DURATION__);
	DisplayMessage(L" .");
	Sleep(__SUSPENSE_DURATION__);
	DisplayMessage(L".");
	Sleep(__SUSPENSE_DURATION__);
	DisplayMessage(L".\n");
	Sleep(__SUSPENSE_DURATION__);
	ClearScreen();
	return true;
}

// color setter
inline void LogManager::SetColor(const WORD& color)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}